package com.sunny.ynhz.ui.index;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.OneRiverOneRuleAdapter;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.OneRiverOneRuleBean;
import com.sunny.ynhz.ui.OneRiverActivity;
import com.sunny.ynhz.ui.base.AppBaseTitleBarActivity;
import com.sunny.ynhz.ui.base.BaseFragment;
import com.sunny.ynhz.utils.ShowUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by luao on 2017/8/20.
 */

public class RankNaviFragment extends BaseFragment {

    @BindView(R.id.lv_rank)
    ListView lv_rank;
    @BindView(R.id.tab_title)
    TabLayout mTab_title;
    List<OneRiverOneRuleBean.ListBean> mList = new ArrayList<>();
    List<OneRiverOneRuleBean.ListBean> xsList = new ArrayList<>();
    private OneRiverOneRuleAdapter oneRiverOneRuleAdapter;

    private String string = "州级河长";

    public static RankNaviFragment getRankNaviFragment() {
        return new RankNaviFragment();
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.lv_rank_msg;
    }

    @Override
    protected void setUpView() {
        super.setUpView();
//        View view = LayoutInflater.from(getContext()).inflate(R.layout.head_yz_yx, null);
//        lv_rank.addHeaderView(view);
        mTab_title.addTab(mTab_title.newTab().setText("州级河长"));
        mTab_title.addTab(mTab_title.newTab().setText("县(市)河长"));
        lv_rank.setOnItemClickListener((adapterView, view1, i, l) -> {
            if (i == 0) {
                return;
            }
            if (string.contains("州")) {
                OneRiverOneRuleBean.ListBean listBean = mList.get(i - 1);
                String id = listBean.getId();
                Intent intent = new Intent(getContext(), OneRiverActivity.class);
                intent.putExtra("id", id);
                doAcitivity(intent);
            } else {
                OneRiverOneRuleBean.ListBean listBean = xsList.get(i - 1);
                String id = listBean.getId();
                Intent intent = new Intent(getContext(), OneRiverActivity.class);
                intent.putExtra("id", id);
                doAcitivity(intent);
            }

        });
        getMsg("1");
        mTab_title.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                string = tab.getText().toString();
                if (string.equals("州级河长")) {
                    if (mList.size() > 0) {
                        oneRiverOneRuleAdapter = new OneRiverOneRuleAdapter(getContext(), mList,1);
                        lv_rank.setAdapter(oneRiverOneRuleAdapter);
                    } else {
                        getMsg("1");
                    }

                } else if (!string.equals("州级河长")) {
                    if (xsList.size() > 0) {
                        oneRiverOneRuleAdapter = new OneRiverOneRuleAdapter(getContext(), xsList,1);
                        lv_rank.setAdapter(oneRiverOneRuleAdapter);
                    } else {
                        getMsg("2");
                    }
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    public void getMsg(String id) {
        HttpManager.getInstance().getListRiverOneRuleMsgForOneArea(id,"HA012").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(getContext(), new ApiSubscriber.HttpOnResultListener<OneRiverOneRuleBean>() {
            @Override
            public void onNext(OneRiverOneRuleBean oneRiverOneRuleBean) {
                List<OneRiverOneRuleBean.ListBean> list = oneRiverOneRuleBean.getList();
                for (int i = 0; i < list.size(); i++) {
                    OneRiverOneRuleBean.ListBean listBean = list.get(i);
                    if (i == 0) {
                        listBean.setRankNum(1);
                    } else {
                        OneRiverOneRuleBean.ListBean listBean1 = list.get(i - 1);
                        if (listBean1.getProgressBar().equals(listBean.getProgressBar())) {
                            listBean.setRankNum(listBean1.getRankNum());
                        } else listBean.setRankNum(i + 1);
                    }
                }
                if (id.equals("1")) {
                    mList.addAll(list);
                    oneRiverOneRuleAdapter = new OneRiverOneRuleAdapter(getContext(), mList,1);
                    lv_rank.setAdapter(oneRiverOneRuleAdapter);
                } else if (id.equals("2")) {
                    xsList.addAll(list);
                    oneRiverOneRuleAdapter = new OneRiverOneRuleAdapter(getContext(), xsList,1);
                    lv_rank.setAdapter(oneRiverOneRuleAdapter);
                }
            }

            @Override
            public void onCompleted() {
                ShowUtils.closePopwindow();
            }

            @Override
            public void onstart() {
                ShowUtils.inItPopwin(getContext(), "加载中");
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                ShowUtils.closePopwindow();
            }
        }));


    }

}
